package businessLogic.facade;

import businessLogic.service.MateriaService;
import entity.Materia;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import vo.IValueObject;
import vo.MateriaVO;

/**
 *
 * @author Alexander
 */
public class MateriaFacade extends Facade{
    
    public MateriaFacade(){
        setService(new MateriaService());
    }

    @Override
    protected boolean exist(IValueObject vo) {
        if (super.find(((MateriaVO) vo).getCodigo()) == null){
            return false;
        } else {
            return true;
        }
    }
    
    public List<MateriaVO> search (Object guess, boolean byCode){
         EntityManagerFactory emf = null;
        EntityManager em = null;
        EntityTransaction tx = null;
        List<MateriaVO> materias = new ArrayList<>();
        try {
            emf = Persistence.createEntityManagerFactory("StunnicusPU");
            em = emf.createEntityManager();
            materias = ((MateriaService)getService()).search(guess, byCode, em);
        } catch (Exception e){
            e.printStackTrace();
        }finally {
            if (em != null) {
                em.clear();
                em.close();
            }
            return materias;
        }
    }
}
